package com.xcc.shopanim.olda;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.xcc.shopanim.BaseFragment;
import com.xcc.shopanim.BuildConfig;
import com.xcc.shopanim.OtherFragment;
import com.xcc.shopanim.R;
import com.xcc.shopanim.adapter.ViewPagerAdapter;
import com.xcc.shopanim.utils.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Main2Fragment extends BaseFragment {
    @BindView(R.id.layoutTop)
    View layoutTop;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    public List<BaseFragment> fragmentList;

    public static Main2Fragment newInstance() {
        Main2Fragment fragment = new Main2Fragment();
        /*Bundle bundle = new Bundle();
        fragment.setArguments(bundle);*/
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mian2, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (onFragmentCallBack != null) {
            //获取头部高度
            Object o = onFragmentCallBack.onFragmentCallBack(this, 3, null);
            if (o instanceof Integer) {
                int notchHeight = (Integer) o;
                ViewGroup.LayoutParams layoutParams = layoutTop.getLayoutParams();
                if (layoutParams == null) {
                    layoutParams = new ViewGroup.LayoutParams(-1, notchHeight);
                } else layoutParams.height = notchHeight;
                layoutTop.setLayoutParams(layoutParams);
            }
        }

        tabLayout.addOnTabSelectedListener(onTabSelectedListener);

        tabLayout.addTab(tabLayout.newTab().setText("全部"));
        tabLayout.addTab(tabLayout.newTab().setText("推荐"));
        tabLayout.addTab(tabLayout.newTab().setText("关注"));

        fragmentList = new ArrayList<>();
        fragmentList.add(MainLiveFragment.newInstance());
        fragmentList.add(OtherFragment.newInstance());
        fragmentList.add(OtherFragment.newInstance());
        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setOnPageChangeListener(onPageChangeListener);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (BuildConfig.DEBUG) Logger.i("--xcc--onHiddenChanged--", "hidden" + hidden);
        int currentItem = viewPager.getCurrentItem();
        if (currentItem < fragmentList.size()) {
            BaseFragment baseFragment = fragmentList.get(currentItem);
            if (baseFragment != null) baseFragment.onHiddenChanged(hidden);
        }
    }

    @OnClick(R.id.imgShopCar)
    public void onImgShopCarClicked(View view) {
        //if (isLogin()) ShopCarActivity.open(getActivity(), view);
    }

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        public void onPageSelected(int position) {
            int tabPosition = tabLayout.getSelectedTabPosition();
            if (tabPosition == position) return;
            TabLayout.Tab tabAt = tabLayout.getTabAt(position);
            if (tabAt == null) return;
            View view = tabAt.view;
            view.performClick();
        }
    };
    private TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int tabPosition = tab.getPosition();
            int position = viewPager.getCurrentItem();
            if (tabPosition == position) return;
            viewPager.setCurrentItem(tabPosition);
        }

        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        BaseFragment fragment = null;
        if (viewPager != null && fragmentList != null) {
            int currentItem = viewPager.getCurrentItem();
            fragment = fragmentList.get(currentItem);
        }
        if (fragment == null) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        } else fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
